package com.fbt.andon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbt.andon.controller.DaysController;
import com.fbt.andon.domain.Days;
import com.fbt.andon.mapper.DaysMapper;
import com.fbt.andon.service.DaysService;
import com.fbt.andon.tool.Direct;
import com.fbt.andon.tool.XMLParse;
import com.rabbitmq.client.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.TimeoutException;

/**
 * <p>
 * 每日产量表 服务实现类
 * </p>
 *
 * @author 冯涛
 * @since 2021-09-28
 */
@Service
public class DaysServiceImpl extends ServiceImpl<DaysMapper, Days> implements DaysService {
    @Autowired
    private DaysMapper daysMapper;





    public void RunMQConsume() throws IOException, TimeoutException {
        /*这里设置连接信息*/
        String host = "127.0.0.1";
        String userName = "guest";
        String passWord = "guest";
        int port = 5672;
        String virtualHost = "/";
        String queueName = "wyj";

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(host);
        factory.setUsername(userName);
        factory.setPassword(passWord);
        factory.setPort(port);
        factory.setVirtualHost(virtualHost);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body, "UTF-8");
                try {
                    Days days1 = XMLParse.analysis1(msg);
                    daysMapper.insert(days1);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
//                System.out.println("消费者获取生产者消息：<" + msg + ">");
            }
        };

        //消费那个队列中的信息
        channel.basicConsume(queueName, true, consumer);

    }

}




